package com.billiard;

import java.awt.Graphics2D;

public class Ball {

	private Point coords;
	
	/** Andle of velocity vector 
	 */
	private double angle;
	private double power;

	private boolean kickChecked;

	private int kick;
	private double kickAngle;
	private double kickPower;
	
	private int number;
	
	public Ball(double x, double y, int num) {
		this.coords = new Point(x, y);
		kickChecked = false;
		this.number = num;
	}
	
	public void paint(Graphics2D g2d)
	{

//		if (!isMoving())
//			return;
		
		// backup old color
//		Color oldColor = g.getColor();
//
//		g2d.setColor(Color.white);
//		g2d.drawOval(coords.getIntX()- R.ballRadius, coords.getIntY() - R.ballRadius, R.ballDiameter, R.ballDiameter);
//
//		g.setColor(Color.RED);
//		g.fillOval(coords.getIntX() - R.ballRadius + (int)(R.ballRadius/3), coords.getIntY() - R.ballRadius + (int)(R.ballRadius/3), (int)(R.ballRadius*1.5), (int)(R.ballRadius*1.5));
//		
//		g.setColor(R.ballColor);
//		g.fillOval(coords.getIntX() - R.ballRadius + (int)(R.ballRadius/2), coords.getIntY() - R.ballRadius + (int)(R.ballRadius/2), R.ballRadius, R.ballRadius);
//
//		// restore old color
//		g.setColor(oldColor);
				
		
		//g2d.drawImage(R.getballImage(this.getClass(), number), coords.getIntX()-R.ballRadius, coords.getIntY()-R.ballRadius, null);
		g2d.drawImage((R.getballImage(this.getClass(), number)), coords.getIntX()-R.ballRadius, coords.getIntY()-R.ballRadius, R.ballDiameter, R.ballDiameter, null);

		
	}
	
	public void move(long time)
	{
		if (power == 0)
			return;
		
		if (kick == R.WALLKick){
			
			double dx = power * Math.cos(angle);
			double dy = power * Math.sin(angle);

			double distance = power;
			
			Point oldPosition = new Point(coords);
			
			// 1. go to wall
			// 2. change angle,
			// 3. go to new position
			if (coords.x - R.ballRadius < Math.abs(dx)) { // WEST Wall
				coords.x = R.ballRadius;
				coords.y += (coords.x - oldPosition.x)*Math.tan(angle);
			}
			else if (R.tableWidth - coords.x - R.ballRadius < Math.abs(dx) ) { // EAST Wall
				coords.x = R.tableWidth - R.ballRadius;
				coords.y += (coords.x - oldPosition.x)*Math.tan(angle);
			} 
			else if (coords.y - R.ballRadius < Math.abs(dy)) { // NORTH Wall
				coords.y = R.ballRadius;
				coords.x += (coords.y - oldPosition.y)/Math.tan(angle);
			}
			else if (R.tableHeight - coords.y - R.ballRadius < Math.abs(dy)) { //SOUTH Wall
				coords.y = R.tableHeight - R.ballRadius;
				coords.x += (coords.y - oldPosition.y)/Math.tan(angle);    
			}//Math.tan(Math.PI + Math.PI/3)

			distance -= Math.abs(oldPosition.distance(coords));

//			System.out.println("x = " + oldPosition.x + ", y = " + oldPosition.y + ", angle = " + angle / Math.PI * 180);
//			System.out.println("x = " + coords.x + ", y = " + coords.y);
			
			angle = kickAngle;
			power = kickPower;
			
			// new angle, new distance
			coords.x += distance * Math.cos(angle); 
			coords.y += distance * Math.sin(angle);
			
			setKick(R.NOKick, 0, 0);
		} 
		else {
			coords.x += power * Math.cos(angle);
			coords.y += power * Math.sin(angle);
		}
		
	}

	
	public boolean isMoving()
	{
		return power > 0;
	}


	public void setPower(double power) {
		this.power = power;
	}

	public Point getCoords() {
		return coords;
	}

	public void setCoords(Point coords) {
		
		this.coords = new Point(coords);
	}

	public void setCoords(double x, double y)
	{
		coords.move(x, y);
	}
	
	/**
	 * Returns the angle of velocity vector
	 * @return angle in radians.
	 */
	public double getAngle() {
		return angle;
	}

	/**
	 * Angle of velocity vector
	 * 
	 * @param angle in radian to be set
	 */
	public void setAngle(double angle) {
		
		this.angle = angle - (((int) (angle/(2*Math.PI)))*Math.PI*2);
	}
	
	public void setKick(int kick, double angle, double power) {
		this.kick      = kick; 
		this.kickAngle = angle;
		this.kickPower = power;
	}

	public double getPower() {
		return power;
	}

	public void setKickPower(double kickPower) {
		this.kickPower = kickPower;
	}

	public void setVelocityVector(double angle, double power)
	{
		this.angle = angle;
		this.power = power;
	}

	public Point getNextPosition()
	{
		Point position = new Point(coords);

		position.x += power * Math.cos(angle);
		position.y += power * Math.sin(angle);
		
		return position;
		
	}
	
	public boolean isKickChecked() {
		return kickChecked;
	}

	public void setKickChecked(boolean kick_checked) {
		this.kickChecked = kick_checked;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	@Override
	public String toString() {
		
		return getClass().getName() + "[x=" + coords.x + ",y=" + coords.y + "], [angle="+angle+",power="+power +"]";
	}
	
}
